<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/18
 * Time: 10:02
 */

namespace app\user\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\zucai\model\Order;
use app\zucai\model\AdminOrder;
use think\Db;

class User extends Admin
{
    /**
     * 跟单设置
     * 吴杰
     */
    public function gendan()
    {

        $data_list = AdminOrder::where('isdraw', '1')
            ->where('type', 0)
            ->where('gendan', 1)			
           ->where('aid', is_signin())
            ->where($this->getMap())
            ->order($this->getOrder('oddtime DESC'))
            ->paginate();
//计算跟单人数及跟单总金额
        for ($i = 0; $i < count($data_list); $i++) {
            $cont = Order::where('pid', $data_list[$i]['uid'])
                ->where('trano', $data_list[$i]['order_id'])
                ->where('isdraw', 1)
                ->where('type', 1)
                ->select();
            $money = 0;
            foreach ($cont as $k => $v) {
                $money += $v['itemcount'];
            }
            $data_list[$i]['number'] = count($cont);
            $data_list[$i]['money'] = $money;
        }
        //计算发单人的中奖金额及胜率等
        for ($i = 0; $i < count($data_list); $i++) {
            $cont = Order::where('uid', $data_list[$i]['uid'])
                ->where('isdraw', 2)
                ->select();
            $tcont = Order::where('uid', $data_list[$i]['uid'])
                ->where('isdraw', 'in','2,3')
                ->count();
            $allmoney = 0;
            $alltou = 0;
            foreach ($cont as $k => $v) {
                $allmoney += $v['mode'];
                $alltou += $v['itemcount'];
            }
            //总中奖金额
            $data_list[$i]['allmoney'] = $allmoney;
            //总投注金额
            $data_list[$i]['alltou'] =$alltou;
            //总投注次数
            $data_list[$i]['allcount'] =count($cont);

            //中奖率
            if($tcont==0){
                $data_list[$i]['pei']='0%';
            }else{
                $data_list[$i]['pei'] =  round((count($cont)/$tcont * 100),2).'%';
            }

        }
//根据跟单总金额排序
            for ($i = 0; $i < count($data_list); $i++) {
                for ($j = 1; $j <count($data_list) ; $j++) {
                    if ($data_list[$i]['money'] < $data_list[$j]['money']) {
                        $iTemp = $data_list[$j];
                        $data_list[$j] = $data_list[$i];
                        $data_list[$i] = $iTemp;
                    }
                }
            }
        return ZBuilder::make('table')
            ->setTableName('order')
            ->addOrder('id,beishu,itemcount')
            ->setSearch(['id' => 'ID', 'username' => '用户名', 'trano' => '订单号'],'','',true)// 设置搜索参数
            ->addColumns([
                ['id', 'ID'],
                ['type', '类型', config('orderplay')],
                ['name', '用户名'],
                ['isdraw', '订单状态', config('ordertype')],
                ['trano', '订单号'],
//                ['caizhong', '彩种'],
//                ['typeid', '赛事编号'],
//                ['playtitle', '玩法'],
  //              ['choice', '竞猜选项'],
                ['choicetype', '玩法'],
                ['beishu', '倍数'],
                ['itemcount', '投注金额'],
                ['number', '跟单人数',],
                ['money', '跟单总金额'],
                ['allcount', '投注次数'],
                ['alltou', '投注总金额'],
                ['allmoney', '总中奖金额'],
                ['pei', '胜率'],
                ['status','跟单展示','switch'],
            ])
            ->addFilter('status', config('ordercon'))
            ->addFilter('dan', config('orderdan'))
            ->addFilter('caizhong')
            ->addFilter('playtitle')
            ->setColumnWidth('id', 70)
			 ->setColumnWidth('name', 120)
            ->setColumnWidth('trano', 140)
            ->setColumnWidth('pid,typeid,choice,allcount', 75)
            ->setColumnWidth('caizhong,beishu,playtitle,type', 65)
            ->setRowList($data_list)
            ->fetch();
    }

}